package com.wjs.gulimall.gulimallorder.listener;

import com.atguigu.common.to.mq.SeckillOrderTo;
import com.rabbitmq.client.Channel;
import com.wjs.gulimall.gulimallorder.entity.OrderEntity;
import com.wjs.gulimall.gulimallorder.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@RabbitListener(queues = "order.seckill.order.queue")
@Component
@Slf4j
public class OrderSecKillListener {
    @Autowired
    private OrderService orderService;
    @RabbitHandler
    public void listener(SeckillOrderTo to, Channel channel, Message message) throws IOException {
        try {
            log.info("收到来自秒杀单的消息{}",to.getOrderSn());
            orderService.createSecKillOrder(to);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            //捕获到异常就进行拒绝重新入队列的操作
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
}
